import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/entity/meta/MetaUserChannel.dart';
import 'package:mahjong/widget/CommonAlterDialog.dart';
import 'package:provider/provider.dart';

typedef _CallBack = void Function(String roomCode);

typedef _BtnCallBack = void Function();

void _defaultOnTap() {}

class Confirm extends StatefulWidget {
  final _CallBack callback;
  final MetaUserChannel userChannel;
  final _BtnCallBack confirm;
  final _BtnCallBack cancel;
  final String tips;

  Confirm(
      {this.callback,
      this.userChannel,
      this.confirm: _defaultOnTap,
      this.cancel: _defaultOnTap,
      this.tips: ""});
  @override
  _ConfirmState createState() => _ConfirmState();
}

class _ConfirmState extends State<Confirm> {
  double dialogHeight, dialogWidth;
  _BtnCallBack _confirm;
  _BtnCallBack _cancel;

  @override
  void initState() {
    super.initState();
    dialogHeight = 120.h;
    dialogWidth = 300.h;
    _confirm = widget.confirm;
    _cancel = widget.cancel;
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
        height: dialogHeight,
        width: dialogWidth,
        child: Consumer(builder:
            (BuildContext buildContext, UserEntityInfo provider, Widget child) {
          return Container(
            padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 30.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.h),
              color: Colors.white,
              /* border: Border.all(color: Colors.orange.withOpacity(0.2)) */
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Text(
                  widget.tips,
                  style: TextStyle(fontSize: 18.sp),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    buildButtion(text: "确定", onTap: _confirm),
                    buildButtion(
                        background: Colors.orange, text: "取消", onTap: _cancel),
                  ],
                )
              ],
            ),
          );
        }));
  }

  Widget buildButtion(
      {double width,
      double height,
      Color background: Colors.green,
      String text: "",
      _BtnCallBack onTap: _defaultOnTap}) {
    return Container(
      height: 30.h,
      width: 70.h,
      child: ElevatedButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            //设置按下时的背景颜色
            if (states.contains(MaterialState.pressed)) {
              return Colors.blue[200];
            }
            //默认不使用背景颜色
            return background;
          }),
          elevation: MaterialStateProperty.all(12),
          shape: MaterialStateProperty.all(StadiumBorder()),
        ),
        child: Text(
          text,
          style: TextStyle(fontSize: 16.sp),
        ),
        onPressed: onTap,
      ),
    );
  }
}
